
public class DataClass{  
  //VARIABLES
  private ArrayList dataRows = null;
  private int lineCount = 0;  
  private ArrayList magnitude = null;
  private ArrayList dateList = null;
  private ArrayList locationList = null;  
  private ArrayList allList = null;

  DataClass()
  {
    dataRows = new ArrayList();
    allList = new ArrayList();
  }

  public void printData(){
    
    for( int i = 0 ; i < dataRows.size() ; i++) {
      DataRow temp = (DataRow) dataRows.get(i);
      println(temp.getYear() + "\t" + temp.getMonth() + "\t" + temp.getDay() + "\t" + temp.getTime());
    }
    
  }
    
  public ArrayList getData() {
    return dataRows;
  }
  
  public ArrayList getSplitData(){
    return dateList;
  }
  public ArrayList getLocationData(){
    return locationList;
  }

  public ArrayList getAllData()
  {
    return allList;
  }

  
  
  
  public int getNumberOfPoints() {
    return dataRows.size();
  }
  
  public Date getMaxDate() {
    Date date =  new Date();
    
    DataRow drow = (DataRow) dataRows.get( dataRows.size() - 1 );

    if(drow != null){
      date.yr = drow.getYear();
      date.mon = drow.getMonth();
      date.dy = drow.getDay();
      date.time = drow.getTime();
      
    }

    return date;
  }
  
  public Date getMinDate() {
    Date date =  new Date();
    
    DataRow drow = (DataRow) dataRows.get( 0 );

    if(drow != null){
      date.yr = drow.getYear();
      date.mon = drow.getMonth();
      date.dy = drow.getDay();
      date.time = drow.getTime();
    }

    return date;    
  }
  
  
 
  public void readData(){
    String inputLine;
    lineCount = -1;
    dataRows.clear();
    
    //Introducing the java BufferedReader class
     //reader = createReader("eqmagsort.csv");
     reader = createReader(fileName);

    
    try{
        
      while( (inputLine = reader.readLine()) != null   ) {
        String[] linePieces;
        lineCount ++;
      //reading lines
        if(inputLine != null){
        linePieces = split(inputLine, ",");
        DataRow dr = new DataRow();
          
        dr.setTimeAndDate( Integer.parseInt ( linePieces[0] ) , Byte.parseByte (linePieces[1]), 
        Byte.parseByte ( linePieces[2] ),  Double.parseDouble ( linePieces[3]));
        dr.setLocation(Double.parseDouble(linePieces[4]), Double.parseDouble(linePieces[5]));
        dr.setMagnitudeAndDepth( Float.parseFloat (linePieces[6]), Integer.parseInt (linePieces[7]));
                
//        if(lineCount % 20000 == 0){
//          println(Integer.parseInt ( linePieces[0] ) );
//        }
  
        dataRows.add(dr);
        }
      }
    }
    catch(IOException ioe){
      println("IOException" + ioe);
    }
  }
  
   /*
  public ArrayList readData(Date startDate, Date endDate){
    
      dateList = new ArrayList();
      lineCount = -1;
      
      for ( int i = 0; i < dataRows.size(); i++){
        DataRow dr = (DataRow) dataRows.get(i);
        Date temp = dr.getDate();
        
        if( temp.compareDate(startDate) == false ){
//          println( temp.yr + " " + temp.mon + " " + temp.dy + " " + temp.time + " " + i );
//          println( startDate.yr + " " + startDate.mon + " " + startDate.dy + " " + startDate.time + " " + i );          
          dateList.add(dr);          
        }
        
      }

      return dateList;
  }      
  
  
  public ArrayList readData(float magn){
    
      magnitude = new ArrayList();
      lineCount = -1;
      
      for ( int i = 0; i < dataRows.size(); i++){
        DataRow dr = (DataRow) dataRows.get(i);
        
        if( dr.getMagnitude() > magn ){
//          println( temp.yr + " " + temp.mon + " " + temp.dy + " " + temp.time + " " + i );
//          println( startDate.yr + " " + startDate.mon + " " + startDate.dy + " " + startDate.time + " " + i );          
          magnitude.add(dr);          
        }
        
      }

      return magnitude;
  }      
  
  public ArrayList readData(float m, ArrayList magn){
    
      magnitude = new ArrayList();
      
      for ( int i = 0; i < magn.size(); i++){
        DataRow dr = (DataRow) magn.get(i);
        
        if( dr.getMagnitude() > m ){
//          println( temp.yr + " " + temp.mon + " " + temp.dy + " " + temp.time + " " + i );
//          println( startDate.yr + " " + startDate.mon + " " + startDate.dy + " " + startDate.time + " " + i );          
          magnitude.add(dr);          
        }
        
      }

      return magnitude;
  }        
  
  */

  
public ArrayList readData(double lat1, double lon1, double lat2, double lon2, float m1,float m2, Date startDate, Date endDate){
    
      allList.clear();
      lineCount = -1;
      //println("dataRows.size()" + dataRows.size() );
      for ( int i = 0; i < dataRows.size(); i++){
        DataRow dr = (DataRow) dataRows.get(i);
            
        double temLat = dr.getLatitude();
        double temLon = dr.getLongitude();
	Date temp = dr.getDate();
        
        
        if( temLat > lat2 && temLat < lat1 && temLon > lon1 && temLon < lon2 && dr.getMagnitude() > m1 && dr.getMagnitude()<m2 && temp.compareDate(startDate,endDate)  )
        {
          //println("temp.yr = "+temp.yr+ ",mon = "+temp.mon+" , day = "+temp.dy+" , mag = "+dr.getMagnitude() );
          allList.add(dr);
        }
        if( allList.size()> ARRAYLIST_SIZE)
        break;
	 }
      return allList;
  }      

public ArrayList readData(double lat, double lon, double radius, float m, Date startDate, Date endDate){
    
      allList = new ArrayList();
      lineCount = -1;
      
      for ( int i = 0; i < dataRows.size(); i++){
        DataRow dr = (DataRow) dataRows.get(i);
        
        double temLat = dr.getLatitude();
        double temLon = dr.getLongitude();
	Date temp = dr.getDate();
        
        if( temLat > (lat - radius) && temLat < (lat + radius) && temLon > (lon - radius) && temLon < (lon + radius) && dr.getMagnitude() > m && temp.compareDate(startDate) == false  )
          allList.add(dr);
	 }

      return allList;
  }      
     



}

class DataRow{

  int yr;
  int mon, dy;
  double time;
  double latitude, longitude; 
  float magnitude;
  int depth;
   
  DataRow(){
    yr = 0;
    mon = dy = 0;
    time = 0;
    latitude = longitude = 0;
    magnitude = 0;
    depth = 0;
   //    printM();
  }
  
  public float getMagnitude(){
    return magnitude;
  }
  
  public Date getDate(){
    Date date = new Date(yr, mon,dy, time);
    return date;
  }
  public int getYear()
  {
    return yr;
  }
  public int getMonth()
  {
    return mon;
  }
  public int getDay(){
    return dy;
  }
  
  public Double getTime(){
    return time;
  }
  public int getDepth()
  {
    return depth;
  }
  
  public void setLocation(double latitude, double longitude){ 
    this.latitude = latitude;
    this.longitude = longitude;
  }
  
  public Location getLocation(){
    Location loc = new Location( (float)latitude, (float)longitude);
    return loc;
  }          
  
  public double getLatitude(){
    return latitude;
  }
  
  public double getLongitude(){
    return longitude;
  }
  
  public void setMagnitudeAndDepth(float magnitude, int depth) {
    this.magnitude = magnitude;
    this.depth = depth;
  }
  
  public void setTimeAndDate(int yr, byte mon, byte dy, double time){
    this.yr = yr;
    this.mon = int(mon);
    this.dy = int(dy);
    this.time = time;
  }
  
}

class Date{
  int yr;
  int mon;
  int dy;
  double time;
  
  //Checks if this date ob is earlier than the input date ob
  
  Date(){
    
  }
  Date(int yr, int mon){
    this.yr = yr;
    this.mon = mon;
    this.dy = 1;
    this.time = 0;
  }
    Date(int yr, int mon, int dy){
    this.yr = yr;
    this.mon = mon;
    this.dy = dy;
    this.time = 0;
  }
 
  
  Date(int yr, int mon, int dy, double time){
    this.yr = yr;
    this.mon = mon;
    this.dy = dy;
    this.time = time;
  }
  
  boolean compareDate(Date toBeCompared){
    if( this.yr < toBeCompared.yr)
      return true;
    else
    if( this.yr == toBeCompared.yr && this.mon < toBeCompared.mon )
      return true;
    else
    if( this.yr == toBeCompared.yr && this.mon == toBeCompared.mon && this.dy < toBeCompared.dy )
      return true;
    else
    if( this.yr == toBeCompared.yr && this.mon == toBeCompared.mon && this.dy == toBeCompared.dy && this.time < toBeCompared.time )
      return true;
    else
      return false;
    
  }
  
  // Find if the data is between start and end, actually we only need to compare year and month
  
    public boolean compareDate(Date start, Date end){
      if( start.yr == end.yr)
      {
        if( this.yr == start.yr && this.mon>start.mon && this.mon<end.mon)
              return true;
        else
            return false;
      }
      else
        {
        
          if( this.yr> start.yr && this.yr< end.yr)
          {
            return true;
          }
          else if( this.yr == start.yr &&this.mon >= start.mon)
          {
              return true;
          }
          else if( this.yr == end.yr && this.mon< end.mon)
          {
              return true;
          }
          else
              return false;   
              
       }
  }
  
}


